package com.web.base.user.util;

import com.blue.jdbc.Page;
import com.web.base.user.message.AddressRequest;
import com.web.base.user.message.AddressResponse;
import com.web.base.user.model.Address;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月26日
 */
public class AddressConvertor
{
	private AddressConvertor()
	{
	}
	
	public static Address toAddress(AddressRequest request)
	{
		if (request == null)
			return null;
		
		Address address = new Address();
		address.setId(request.getId());
		address.setUserId(request.getUserId());
		address.setProvinceId(request.getProvinceId());
		address.setCityId(request.getCityId());
		address.setAreaId(request.getAreaId());
		address.setName(request.getName());
		address.setConsignee(request.getConsignee());
		address.setAddress(request.getAddress());
		address.setMobile(request.getMobile());
		address.setEmail(request.getEmail());
		address.setState(request.getState());
		address.setDef(request.getDef());
		return address;
	}
	
	public static List<Address> toAddress(List<AddressRequest> requestList)
	{
		List<Address> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (AddressRequest request : requestList)
		{
			list.add(toAddress(request));
		}
		return list;
	}
	
	public static AddressResponse toResponse(Address address)
	{
		if (address == null)
			return null;
		
		AddressResponse response = new AddressResponse();
		response.setId(address.getId());
		response.setUserId(address.getUserId());
		response.setProvinceId(address.getProvinceId());
		response.setCityId(address.getCityId());
		response.setAreaId(address.getAreaId());
		response.setName(address.getName());
		response.setConsignee(address.getConsignee());
		response.setAddress(address.getAddress());
		response.setMobile(address.getMobile());
		response.setEmail(address.getEmail());
		response.setState(address.getState());
		response.setDef(address.getDef());
		return response;
	}
	
	public static List<AddressResponse> toResponse(List<Address> list)
	{
		List<AddressResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Address address : list)
		{
			responseList.add(toResponse(address));
		}
		return responseList;
	}
	
	public static Map<Integer, AddressResponse> toResponse(Map<Integer, Address> map)
	{
		Map<Integer, AddressResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Address> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<AddressResponse> toResponse(Page<Address> page)
	{
		if (page == null)
			return null;
		
		Page<AddressResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
